<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>

        window.onload = function () {
            var elementById = document.getElementsByClassName("aa");
            for (var i = 0; i <elementById.length ; i++) {
                elementById[i].onclick = function () {
                    this.style.color = "red"
                }
            }

        }

    </script>
</head>
<body>

<span class="aa">
    aaaa
</span>
<span class="aa">
    bbbb
</span>


</body>
</html>